﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using DeVetma.admin.classes;
using DeVetma.admin.models.entities;
using DeVetma.admin.models.managers;
using System.Web.Services;

namespace DeVetma.admin
{
    public partial class addClothes : System.Web.UI.Page
    {
        protected bool isSubmitted = false;
        protected string submittedResponse = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            loadBoundedData();

            if (this.IsPostBack)
                processSubmitedForm();   
        }

        private void processSubmitedForm()
        {
            ClothesManager cm = new ClothesManager();
            Clothes clothes = new Clothes();

            clothes.cType = cType.SelectedValue;
            clothes.cDescription = cDescription.Text;
            clothes.cColor = cColor.SelectedValue;
            clothes.cMaterial = cMaterial.SelectedValue;
            clothes.cHips = cHips.Text;
            clothes.cLength = cLength.Text;
            clothes.cShoulder = cShoulder.Text;
            clothes.cWaist = cWaist.Text;
            clothes.cCost = Convert.ToDecimal(cCost.Text);
            clothes.cSelling = Convert.ToDecimal(cSelling.Text);
            clothes.cQuanOnHand = Convert.ToInt32(cQuanOnHand.Text);
            clothes.cStatus = clothes.status(Clothes.ClothesStatus.Available);

            foreach (var control in this.Form.Controls)
            {
                if (control is FileUpload)
                {
                    var fupl = control as FileUpload;
                    if (fupl.HasFile)
                    {
                        clothes.GetType().GetProperty(fupl.ID).SetValue(clothes, fupl.FileName, null);
                        fupl.SaveAs(Server.MapPath("\\admin\\contents\\" + fupl.FileName));
                    }
                }
            }

            submittedResponse = cm.add(clothes) ? "The apparel with id " +
                clothes.cID + " has been successfully added." : "Unable to add the apparel. " +
                "Please verify the input data or contact administrator";
            isSubmitted = true;
        }

        private void loadBoundedData()
        {
            cType.DataSource = new Clothes.ClothesType().enumToDictionary(); //Enum.GetValues(typeof(Clothes.ClothesType));
            cType.DataTextField = "Value";
            cType.DataValueField = "Key";
            cType.DataBind();

            cColor.DataSource = new SettingsManager().getAllClothesColors();
            cColor.DataBind();

            cMaterial.DataSource = new SettingsManager().getAllClothesMaterials();
            cMaterial.DataBind();
        }
    }
}